home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / SwingGraphics.java < prev    next >
Text File  |  1998-06-30  |  11KB  |  372 lines

  1. /*
  2.  * @(#)SwingGraphics.java    1.9 98/04/08
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing;
  21.  
  22. /**
  23.  * A private graphics to access clip bounds without creating a new 
  24.  * rectangle
  25.  *
  26.  * @version 1.9 04/08/98
  27.  * @author Arnaud Weber
  28.  */
  29.  
  30. import java.awt.*;
  31. import java.awt.image.*;
  32. import java.util.Vector;
  33.  
  34. class SwingGraphics extends Graphics {
  35.     Graphics graphics;
  36.     Rectangle clipRect;
  37.     static final int poolSize = 20;
  38.  
  39.     public Graphics create() {
  40.         return createSwingGraphics(graphics.create());
  41.     }
  42.  
  43.     public Graphics subGraphics() {
  44.         return graphics;
  45.     }
  46.  
  47.     SwingGraphics(Graphics g) {
  48.         //        System.out.println("Need new SwingGraphics");
  49.         //        Thread.currentThread().dumpStack();
  50.         init(g);
  51.     }
  52.     
  53.     void init(Graphics g) {
  54.         if(g == null) {
  55.             Thread.currentThread().dumpStack();
  56.         }
  57.         graphics = g;
  58.         clipRect = g.getClipBounds();
  59.         if(clipRect == null)
  60.             clipRect = new Rectangle(0,0,Integer.MAX_VALUE,Integer.MAX_VALUE);
  61.     }
  62.  
  63.     public static SwingGraphics createSwingGraphics(Graphics g) {
  64.         if(g == null)
  65.             return null;
  66.         if(g instanceof SwingGraphics)
  67.             return (SwingGraphics)g;
  68.         else {
  69.             SwingGraphics sg = getRecycledSwingGraphics();
  70.             if(sg != null) 
  71.                 sg.init(g);
  72.             else
  73.                 sg = new SwingGraphics(g);
  74.             return sg;
  75.         }
  76.     }
  77.  
  78.     public Graphics create(int x,int y,int w,int h) {
  79.         return createSwingGraphics(createGraphics(graphics, x, y, w, h));
  80.     }
  81.  
  82.     // This variable does not have to be stored in the AppContext.
  83.     private static int needsSetClip = -1;  // tri-state: not initialized.
  84.  
  85.     /**
  86.      * Create a Graphics from another Graphics object, and set its clip
  87.      * to be the intersection of the first Graphics object's clip rect.
  88.      * Graphics.create() normally does this, but Microsoft's SDK for Java
  89.      * 2.0 doesn't set the clip of the returned object.  Since this method
  90.      * is supposed to emulate what Graphics.create() does, all potential
  91.      * bugs should be first checked with that method before changing the
  92.      * behavior here.
  93.      */
  94.     static Graphics createGraphics(Graphics g, int x, int y, 
  95.                                    int width, int height) {
  96.         Graphics cg = g.create(x, y, width, height);
  97.  
  98.         if (needsSetClip != 0) {
  99.             Rectangle r = g.getClipBounds();
  100.             int X = (r.x > x) ? (r.x - x) : 0;
  101.             int Y = (r.y > y) ? (r.y - y) : 0;
  102.             int W = Math.min(x + width, r.x + r.width) - Math.max(x, r.x);
  103.             int H = Math.min(y + height, r.y + r.height) - Math.max(y, r.y);
  104.  
  105.             // Initialize needsSetClip first time.
  106.             if (needsSetClip == -1 && (x > 0 || y > 0)) {
  107.                 Rectangle r2 = cg.getClipBounds();
  108.                 if (r2.x != X || r2.y != Y || 
  109.                     r2.width != W || r2.height != H) {
  110.                     needsSetClip = 1;
  111.                 } else {
  112.                     needsSetClip = 0;
  113.                 }
  114.             }
  115.  
  116.             cg.setClip(X, Y, W, H);
  117.         }
  118.  
  119.         return cg;
  120.     }
  121.  
  122.     public void translate(int x,int y) {
  123.         graphics.translate(x,y);
  124.     }
  125.  
  126.     public Color getColor() {
  127.         return graphics.getColor();
  128.     }
  129.  
  130.     public void setColor(Color c) {
  131.         graphics.setColor(c);
  132.     }
  133.  
  134.     public void setPaintMode() {
  135.         graphics.setPaintMode();
  136.     }
  137.  
  138.     public void setXORMode(Color c1) {
  139.         graphics.setXORMode(c1);
  140.     }
  141.  
  142.     public Font getFont() {
  143.         return graphics.getFont();
  144.     }
  145.  
  146.     public void setFont(Font font) {
  147.         graphics.setFont(font);
  148.     }
  149.     
  150.     public FontMetrics getFontMetrics(Font f) {
  151.         return graphics.getFontMetrics(f);
  152.     }
  153.  
  154.     public Rectangle getClipBounds() {
  155.         return graphics.getClipBounds();
  156.     }
  157.  
  158.     public boolean isClipIntersecting(Rectangle r) {
  159.         if (clipRect.x >= r.x + r.width || clipRect.x + clipRect.width <= r.x ||
  160.             clipRect.y >= r.y + r.height || clipRect.y + clipRect.height <= r.y) {
  161.             return false;
  162.         }
  163.         return !(clipRect.width == 0 || clipRect.height == 0 || r.width == 0 ||
  164.                  r.height == 0);
  165.     }
  166.  
  167.     public int getClipX() {
  168.         return clipRect.x;
  169.     }
  170.  
  171.     public int getClipY() {
  172.         return clipRect.y;
  173.     }
  174.  
  175.     public int getClipWidth() {
  176.         return clipRect.width;
  177.     }
  178.  
  179.     public int getClipHeight() {
  180.         return clipRect.height;
  181.     }
  182.  
  183.     public void clipRect(int x, int y, int width, int height) {
  184.         graphics.clipRect(x,y,width,height);
  185.         _changeClip(x,y,width,height,false);
  186.     }
  187.  
  188.     public void setClip(int x, int y, int width, int height) {
  189.         graphics.setClip(x,y,width,height);
  190.     _changeClip(x, y, width, height, true);
  191.     }
  192.  
  193.     public Shape getClip() {
  194.         return graphics.getClip();
  195.     }
  196.     
  197.     public void setClip(Shape clip) {
  198.         graphics.setClip(clip);
  199.         if(clip instanceof Rectangle) {
  200.             Rectangle r = (Rectangle) clip;
  201.             _changeClip(r.x,r.y,r.width,r.height,true);
  202.         }
  203.     }
  204.  
  205.     public void copyArea(int x, int y, int width, int height,
  206.                          int dx, int dy) {
  207.         graphics.copyArea(x,y,width,height,dx,dy);
  208.     }
  209.  
  210.     public void drawLine(int x1, int y1, int x2, int y2) {
  211.         graphics.drawLine(x1,y1,x2,y2);
  212.     }
  213.  
  214.     public void fillRect(int x, int y, int width, int height) {
  215.         graphics.fillRect(x,y,width,height);
  216.     }
  217.     
  218.     public void clearRect(int x, int y, int width, int height) {
  219.         graphics.clearRect(x,y,width,height);
  220.     }
  221.  
  222.     public void drawRoundRect(int x, int y, int width, int height,
  223.                               int arcWidth, int arcHeight) {
  224.         graphics.drawRoundRect(x,y,width,height,arcWidth,arcHeight);
  225.     }
  226.  
  227.     public void fillRoundRect(int x, int y, int width, int height,
  228.                               int arcWidth, int arcHeight) {
  229.         graphics.fillRoundRect(x,y,width,height,arcWidth,arcHeight);
  230.     }
  231.     
  232.     public void drawOval(int x, int y, int width, int height) {
  233.         graphics.drawOval(x,y,width,height);
  234.     }
  235.  
  236.     public void fillOval(int x, int y, int width, int height) {
  237.         graphics.fillOval(x,y,width,height);
  238.     }
  239.  
  240.     public void drawArc(int x, int y, int width, int height,
  241.                         int startAngle, int arcAngle) {
  242.         graphics.drawArc(x,y,width,height,startAngle,arcAngle);
  243.     }
  244.  
  245.     public void fillArc(int x, int y, int width, int height,
  246.                         int startAngle, int arcAngle) {
  247.         graphics.fillArc(x,y,width,height,startAngle,arcAngle);
  248.     }
  249.  
  250.     public void drawPolyline(int xPoints[], int yPoints[],
  251.                              int nPoints) {
  252.         graphics.drawPolyline(xPoints,yPoints,nPoints);
  253.     }
  254.  
  255.     public void drawPolygon(int xPoints[], int yPoints[],
  256.                             int nPoints) {
  257.         graphics.drawPolygon(xPoints,yPoints,nPoints);
  258.     }
  259.  
  260.     public void fillPolygon(int xPoints[], int yPoints[],
  261.                             int nPoints) {
  262.         graphics.fillPolygon(xPoints,yPoints,nPoints);
  263.     }
  264.  
  265.     public void drawString(String str, int x, int y) {
  266.         graphics.drawString(str,x,y);
  267.     }
  268.  
  269.     public boolean drawImage(Image img, int x, int y, 
  270.                              ImageObserver observer) {
  271.         return graphics.drawImage(img,x,y,observer);
  272.     }
  273.  
  274.     public boolean drawImage(Image img, int x, int y,
  275.                              int width, int height, 
  276.                              ImageObserver observer) {
  277.         return graphics.drawImage(img,x,y,width,height,observer);
  278.     }
  279.  
  280.     public boolean drawImage(Image img, int x, int y, 
  281.                              Color bgcolor,
  282.                              ImageObserver observer) {
  283.         return graphics.drawImage(img,x,y,bgcolor,observer);
  284.     }
  285.  
  286.     public boolean drawImage(Image img, int x, int y,
  287.                              int width, int height, 
  288.                              Color bgcolor,
  289.                              ImageObserver observer) {
  290.         return graphics.drawImage(img,x,y,width,height,bgcolor,observer);
  291.     }
  292.  
  293.     public boolean drawImage(Image img,
  294.                              int dx1, int dy1, int dx2, int dy2,
  295.                              int sx1, int sy1, int sx2, int sy2,
  296.                              ImageObserver observer) {
  297.         return graphics.drawImage(img,dx1,dy1,dx2,dy2,sx1,sy1,sx2,sy2,observer);
  298.     }
  299.  
  300.     public boolean drawImage(Image img,
  301.                              int dx1, int dy1, int dx2, int dy2,
  302.                              int sx1, int sy1, int sx2, int sy2,
  303.                              Color bgcolor,
  304.                              ImageObserver observer) {
  305.         return graphics.drawImage(img,dx1,dy1,dx2,dy2,sx1,sy1,sx2,sy2,bgcolor,observer);
  306.     }
  307.     
  308.     public void dispose() {
  309.         if(graphics != null) {
  310.             graphics.dispose();
  311.             graphics = null;
  312.         }
  313.         recycle();
  314.     }
  315.  
  316.     public void recycle() {
  317.         graphics = null;
  318.         SwingGraphics.recycleSwingGraphics(this);
  319.     }
  320.  
  321.     private void _changeClip(int x,int y,int w,int h,boolean set) {
  322.         if(set) {
  323.             clipRect.x = x;
  324.             clipRect.y = y;
  325.             clipRect.width = w;
  326.             clipRect.height = h;
  327.         } else 
  328.             SwingUtilities.computeIntersection(x,y,w,h,clipRect);
  329.     }
  330.  
  331.     private static final Object graphicsPoolKey = SwingGraphics.class;
  332.  
  333.     private static synchronized void recycleSwingGraphics(SwingGraphics g) {
  334.         Vector pool = (Vector)SwingUtilities.appContextGet(graphicsPoolKey);
  335.         if (pool == null) {
  336.             pool = new Vector(poolSize);
  337.             SwingUtilities.appContextPut(graphicsPoolKey, pool);
  338.         }
  339.         if(pool.size() < poolSize) {
  340.             /**       int index = pool.indexOf(g);
  341.             if(index != -1) {
  342.                 System.out.println("Tried to recycle the same graphics twice!");
  343.                 Thread.currentThread().dumpStack();
  344.                 while(true);
  345.             }**/
  346.             pool.addElement(g);
  347.         }
  348.         //  System.out.println("Pool size is now " + pool.size());
  349.     }
  350.     
  351.     private static synchronized SwingGraphics getRecycledSwingGraphics() {
  352.         int size;
  353.         SwingGraphics r = null;
  354.         Vector pool = (Vector)SwingUtilities.appContextGet(graphicsPoolKey);
  355.         if(pool == null)
  356.             return null;
  357.         else if((size = pool.size()) > 0) {
  358.             r = (SwingGraphics) pool.elementAt(size - 1);
  359.             pool.removeElementAt(size - 1);
  360.         }
  361.         return r;
  362.     }
  363. }
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.